﻿var newsId = 0;
$.fn.datebox.defaults.formatter = function (date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '/' + (m < 10 ? ('0' + m) : m) + '/' + (d < 10 ? ('0' + d) : d);
};
$.fn.datebox.defaults.parser = function (s) {
    if (!s) return new Date();
    var ss = s.split('/');
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
};
$(function () {
    var h = $('#mytt').height();
    $('.myh').height(h - 50);
    InitGird();
    InitSearch();
});

//初始化表格
function InitGird() {
    $('#tt').datagrid({
        title: '通知公告', //表格标题
        url: 'GetNews.ashx', //请求数据的页面
        sortName: 'newsTime', //排序字段
        idField: 'newsId', //标识字段,主键
        iconCls: '', //标题左边的图标
        width: '100%', //宽度
        //height: $(parent.document).find("#mainPanle").height() - 35 > 0 ? $(parent.document).find("#mainPanle").height() - 35 : 500, //高度
        nowrap: false, //是否换行，True 就会把数据显示在一行里
        striped: true, //True 奇偶行使用不同背景色
        collapsible: false, //可折叠
        sortOrder: 'desc', //排序类型
        remoteSort: true, //定义是否从服务器给数据排序
        fitColumns: true,
        singleSelect: true,
        method: 'post',
        columns: [[
        { field: 'ckb', checkbox: true },
        { title: '标题', field: 'newsTitle', width: 80, sortable: true },
        {
            title: '内容简介', field: 'newsContent', width: 100, formatter: function (value, row, index) {
                return subContent(row.newsContent);
            }
        },
        { title: '发布时间', field: 'newsTime', width: 150 },
        { title: '作者', field: 'newsAuthor', width: 50 }

        ]],
        toolbar: "#tab_toolbar",
        queryParams: { "action": "queryfilesearch" },
        pagination: true, //是否开启分页
        pageNumber: 1, //默认索引页
        pageSize: 20, //默认一页数据条数
        rownumbers: true //行号
    });


}

function importFiles() {
    var f = $('#fm');
    f.form('submit', {
        url: "GetNews.ashx?action=import",
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            var parter = /(\\n+)/g;
            result = result.replace(parter, "<br/>");
            $('#dlgimport').dialog('close');
            $.messager.alert("提示", result, "info");
            //$('#tt').datagrid('reload');
        }
    });
}
//截取字符串
function subContent(str){
    if (str.length > 20) {
        return str.substring(0,19) + "...";
    }
    else {
        return str;
    }
}

//初始化搜索框
function InitSearch() {
    $("#ipt_search").searchbox({
        width: 200,
        //iconCls: 'icon-save',
        searcher: function (val, name) {
            $('#tt').datagrid('options').queryParams.search_type = name;
            $('#tt').datagrid('options').queryParams.search_value = val;
            $('#tt').datagrid('reload');
        },
        prompt: '请输入查询信息'
    });

}

function newUser() {
    //清空内容  
    $('#fm').form('clear');
    $('#dlg').dialog('open').dialog('setTitle', '添加');
    document.getElementById("test").value = "addfilesearch";
}
//修改管理员  
function editUser() {

    var row = $('#tt').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要修改的行！", "info");
    }


    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        $('#newsId').val(row.newsId);
        document.getElementById("test").value = "modify";
        $('#fm').form('load', row);

    }
}
//添加        
function saveUser() {
    var test = document.getElementById("test").value;
    var f = $('#fm');
    if (test == "addfilesearch") {
        f.form('submit', {
            url: "GetNews.ashx?action=" + test,
            onSubmit: function () {
                //return $(this).form('validate');

                return true;
            },
            success: function (result) {
                if (result.indexOf("T") == 0) {
                    $('#dlg').dialog('close');
                    $.messager.alert("提示", "恭喜您，信息添加成功", "info");
                    $('#tt').datagrid('reload');
                }
                else if ((result.indexOf("F") == 0)) {
                    $.messager.alert("提示", "添加失败，请重新操作！", "info");
                    return;
                }
                else if ((result.indexOf("L") == 0)) {
                    $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                    top.location = '/Default.aspx';
                    return;
                }
            }
        });

    } else {

        $('#fm').form('submit', {
            url: "GetNews.ashx?action=" + test,
            onSubmit: function () {
                //return $(this).form('validate');
                return true;
            },
            success: function (result) {
                if (result.indexOf("T") == 0) {
                    $('#dlg').dialog('close');
                    $('#tt').datagrid('clearSelections');
                    $.messager.alert("提示", "恭喜您，信息修改成功", "info");
                    $('#tt').datagrid('reload');

                }
                else if ((result.indexOf("F") == 0)) {
                    $.messager.alert("提示", "修改失败，请重新操作！", "info");
                    return;
                }
                else if ((result.indexOf("L") == 0)) {
                    $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                    top.location = '/Default.aspx';
                    return;
                }
            }
        });
    }
}

function removeUser() {
    var test = document.getElementById("test").value = "delete";
    var row = $('#tt').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要删除的行！", "info");
    }
    if (row) {
        $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
            if (r) {
                if (r.drelation == '1') {
                    $.messager.alert("提示", "用户有党团关系不能删除！", "info");
                    return;
                }
                if (r.hrelation == '1') {
                    $.messager.alert("提示", "用户有户籍关系不能删除！", "info");
                    return;
                }
                if (r.sbrelation == '1') {
                    $.messager.alert("提示", "用户有社保关系不能删除！", "info");
                    return;
                }
                $('#fm').form('submit', {
                    url: 'GetNews.ashx?newsId=' + row.newsId + "&action=" + test,
                    onSubmit: function () {
                        //return $(this).form('validate');  
                    },
                    success: function (result) {
                        if (result.indexOf("T") == 0) {
                            $('#dlg').dialog('close');
                            $('#tt').datagrid('clearSelections'); //清空选中的行  
                            $.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                            //alert('恭喜您，信息删除成功！')  
                            // close the dialog  
                            $('#tt').datagrid('reload');
                            //$('#fm').form('submit');

                        }
                        else if ((result.indexOf("F") == 0)) {
                            $.messager.alert("提示", "删除失败，请重新操作！", "info");
                            return;
                        }
                        else if ((result.indexOf("L") == 0)) {
                            $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                            top.location = '/Default.aspx';
                            return;
                        }

                    }

                });
            }
        })
    }
}